package com.blood.modules.pregnancy.service.impl;

import cn.hutool.core.lang.Snowflake;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.blood.core.config.mybatisplus.ICommonServiceImpl;
import com.blood.core.enums.RuleTypeEnum;
import com.blood.core.enums.StatusEnum;
import com.blood.core.resp.GlobalResultStatus;
import com.blood.core.resp.ResponseResult;
import com.blood.core.util.ObjectAssertUtil;
import com.blood.core.util.bean.ListUtils;
import com.blood.modules.pregnancy.dto.req.PregnancyRuleReq;
import com.blood.modules.pregnancy.dto.req.PregnancyRuleSaveReq;
import com.blood.modules.pregnancy.dto.resp.PregnancyRuleInfoResp;
import com.blood.modules.pregnancy.dto.resp.PregnancyRuleListResp;
import com.blood.modules.pregnancy.mapper.PregnancyRuleMapper;
import com.blood.modules.pregnancy.model.PregnancyRule;
import com.blood.modules.pregnancy.service.IPregnancyRuleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 孕产规则Service业务层处理
 *
 * @author daoren
 * @date 2022-11-24 13:39:22
 */
@Service
public class PregnancyRuleServiceImpl extends ICommonServiceImpl<PregnancyRuleMapper, PregnancyRule> implements IPregnancyRuleService {
    @Autowired
    private Snowflake snowFlake;


    @Override
    public ResponseResult<PregnancyRuleListResp> queryListInfo(PregnancyRuleReq req) {
        LambdaQueryWrapper<PregnancyRule> lambda = new QueryWrapper<PregnancyRule>().lambda();
        if (StringUtils.isNotBlank(req.getClassifyName())) {
            lambda.eq(PregnancyRule::getClassifyName, req.getClassifyName());
        }
        if (StringUtils.isNotBlank(req.getName())) {
            lambda.like(PregnancyRule::getName, req.getName());
        }
        List<PregnancyRule> pregnancyRules = baseMapper.selectList(lambda
                .eq(PregnancyRule::isDeleteFlag, false));
        PregnancyRuleListResp listResp = new PregnancyRuleListResp();
        if (pregnancyRules.size() == 0) {
            listResp.setRuleList(Collections.emptyList());
            listResp.setDealList(Collections.emptyList());

        } else {
            listResp.setRuleList(ListUtils.copyProperties(pregnancyRules.stream().filter(f -> f.getType().equals(RuleTypeEnum.Rule.getValue())).collect(Collectors.toList()), PregnancyRuleInfoResp.class));
            listResp.setDealList(ListUtils.copyProperties(pregnancyRules.stream().filter(f -> f.getType().equals(RuleTypeEnum.DISPOSE.getValue())).collect(Collectors.toList()), PregnancyRuleInfoResp.class));
        }
        return GlobalResultStatus.SUCCESS.toResult().response(listResp);
    }

    @Override
    public List<PregnancyRule> queryList(Integer type) {
        return baseMapper.selectList(new QueryWrapper<PregnancyRule>().lambda()
                .eq(PregnancyRule::getType, type)
                .eq(PregnancyRule::getStatus, StatusEnum.ENABLE.getValue())
                .eq(PregnancyRule::isDeleteFlag, false));
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult<Void> updateInfo(PregnancyRuleSaveReq req) {
        PregnancyRule queryInfo = super.findById(req.getId());
        ObjectAssertUtil.objectIsNull(queryInfo, "未获取到详细信息!");
        queryInfo.setStatus(req.getStatus());
        baseMapper.updateById(queryInfo);
        return GlobalResultStatus.SUCCESS.toResult().responseEmptyBody();
    }
}
